package com.lemon.bean.stageBean;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import static com.lemon.beanManager.BeanContainer.beanFactory;

public class LoadMutiRename {
    //多选重命名窗口
    private Pane mutiRenameWindow;
    private Stage stage = new Stage();
    public void loadView(){
        try {
            mutiRenameWindow = FXMLLoader.load(getClass().getResource("MutiRename.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(mutiRenameWindow,330,200);
        stage.setScene(scene);
        stage.setWidth(330);
        stage.setHeight(200);
        stage.initStyle(StageStyle.UNDECORATED);
    }
    public void initialFunction(){
        TextField mutiFontName = (TextField) beanFactory.getBean("mutiFontName");
        TextField mutiStartIndex = (TextField) beanFactory.getBean("mutiStartIndex");
        TextField mutiPlaceCount = (TextField) beanFactory.getBean("mutiPlaceCount");
        Button mutiConfirm = (Button) beanFactory.getBean("mutiConfirm");
        Button mutiCancel = (Button) beanFactory.getBean("mutiCancel");
        mutiCancel.setOnAction(e->{
            close();
        });
        mutiConfirm.setOnAction(e->{
            String fontName = mutiFontName.getText();
            int len = waitRenameTextList.size();
            int start = 0;
            int placeCount=1;
            try {
                start = Integer.parseInt(mutiStartIndex.getText());
            } catch (NumberFormatException ex) {
                System.err.println("请注意你的言辞");
                start=0;
            }
            try {
                placeCount=Integer.parseInt(mutiPlaceCount.getText());
            }catch (NumberFormatException ex){
                System.err.println("请注意你的言辞");
                placeCount=1;
            }
            for(int i =0 ;i<len;i++){
                Text text = waitRenameTextList.get(i);
                String filename = waitRenameFilenameList.get(i);
                String fileType = text.getText().substring(text.getText().lastIndexOf("."));
                String newName = fontName + formatNumber(placeCount, i + start) + fileType;
                text.setText(newName);
                File oldFile = new File(filename);
                String filePath = filename.substring(0, filename.lastIndexOf("/"));

                File newFile = new File(filePath +"/"+ newName);
                boolean b = oldFile.renameTo(newFile);
                ImageView imageView = (ImageView) text.getParent().lookup("ImageView");
                imageView.setImage(new Image("file:"+newFile.getAbsolutePath()));

                if(!b){
                    System.out.println("改名失败");
                }
            }
            close();
        });

        URL resource = this.getClass().getResource("css/mutiRename.css");
        addStyle(resource.toExternalForm());
    }



    private String formatNumber(int placeCount,int i){
        String str="";
        int num = (int) (Math.log10(i)+1);
        for(int j=0;j<placeCount-num;j++){
            str+="0";
        }
        return str+i;
    }
    public void show(){
        stage.show();
    }
    public void close(){
        beanFactory.remove("mutiFontName");
        beanFactory.remove("mutiStartIndex");
        beanFactory.remove("mutiPlaceCount");
        beanFactory.remove("mutiConfirm");
        beanFactory.remove("mutiCancel");
        stage.close();
    }
    private List<Text> waitRenameTextList;
    private List<String> waitRenameFilenameList;
    public void setWaitRenameTextList(List<Text> waitRenameTextList){
        this.waitRenameTextList=waitRenameTextList;
    }
    public void setWaitRenameFilenameList(List<String> waitRenameFilenameList){
        this.waitRenameFilenameList=waitRenameFilenameList;
    }
    private void addStyle(String cssPath) {
        mutiRenameWindow.getStylesheets().add(cssPath);
    }
}
